// pages/order/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading:0,
    page:1,
    status:0,
    lastpage:0,
    listData:[]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ extConfig: app.getExtConfigSync() })
    if (options.status){
      this.data.status = options.status;
      this.setData({ status: options.status });
    }
    this.loadOderList();
  },
  setStatus:function(e){
    var status = e.currentTarget.dataset.status;
    if (this.data.status == status){
      return ;
    }
    this.setData({ status: status});
    this.loadOderList(1);
    //console.log(e);
  },
  //去支付
  topay:function(e){
    var order_sn = e.currentTarget.dataset.order_sn;
    var that = this;
    app.api("api/pay/topay", {
      order_sn: order_sn
    }, function (res) {
      if (res.code != 200) {
        app.alert(res.msg);
        that.data.payloading = 0;
        that.setData({ 'loading': 0 });
        that.setData({ 'disabled': 0 });
        return false;
      }
      if (res.data.over == 1) {
        that.setData({ loading: 0, disabled: 0 });
        wx.navigateTo({
          url: '/pages/order_offline/detail?order_sn=' + order_sn,
        })
        return;
      }
      var params = res.data;
      //params.total_fee = 0.1;
      params.success = function (res) {
        that.setData({ loading: 0, disabled: 0 });
        wx.navigateTo({
          url: '/pages/order_offline/detail?order_sn=' + order_sn,
        })
      }
      params.fail = function (res) {
        console.log(res);
        if (res.errMsg == 'requestPayment:fail cancel') {
          that.setData({ loading: 0, disabled: 0 });
          return false;
        }
        app.alert(res.errMsg);
      }
      console.log(params);
      wx.requestPayment(params)
    })
  },
  
  loadOderList:function(reload){
    var that = this;
   
    if (reload == 1) {
      this.setData({
        loading: 0,
        page: 1,
        lastpage: 0,
        listData: [],
      });
    }

    var page = this.data.page;
    if (this.data.loading == 1 || this.data.lastpage == 1) {
      //console.log(this.data);
      return;
    }
    this.data.loading = 1;
    app.api("mall/order_offline/get", {
      page: page,
      limit: 10,
      status: this.data.status,
      order: 'id desc'
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (res.data.list.length == 0) {
        that.data.lastpage = 1;
      }
      that.data.page++;
      that.setData({
        listData: that.data.listData.concat(res.data.list),
      });
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var reload = app.getPageData("group/index");
    if (reload==1){
      this.loadOderList(1);
      app.setPageData("group/index", { reload: 0 });
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadOderList(1);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadOderList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})